Parser: Improve extract.c to strip <% # comments %> with space#825
Merged
Conversation
marcoroth
added a commit
that referenced
this pull request
Nov 9, 2025
Follow up to #825. This pull request improves the engine by not compiling `<% # coments %> comments as part of the template. Outputting `# coments` could lead to some cases, where the template could be compiled but couldn't be rendered, because it would comment out important pieces of the template. ```erb <% if true %><% # comment %><%= "Hello World" %><% end %> ``` **Before** `exe/herb compile test.html.erb` ```ruby __herb = ::Herb::Engine; _buf = ::String.new; if true; # comment _buf << __herb.h(("Hello World")); end; _buf << ' '.freeze; _buf.to_s ``` **After** `exe/herb compile test.html.erb` ```ruby __herb = ::Herb::Engine; _buf = ::String.new; if true; _buf << __herb.h(("Hello World")); end; _buf << ' '.freeze; _buf.to_s ``` ### For reference `bin/erubi-compile test.html.erb` ```ruby __erubi = ::Erubi; _buf = ::String.new; if true ; # comment ; _buf << __erubi.h(( "Hello World" )); end ; _buf << ' '.freeze; _buf.to_s ``` `bin/erubi-render test.html.erb` ``` bin/erubi-render:117:in 'Kernel#eval': (eval at bin/erubi-render:117):2: syntax errors found (SyntaxError) 1 | __erubi = ::Erubi; _buf = ::String.new; if true ; # comment ; _buf << __erubi.h(( "Hello World" )); end ; _buf << ' > 2 | '.freeze; | ^ unterminated string meets end of file 3 | _buf.to_s > 4 | | ^ expected an `end` to close the conditional clause | ^ unexpected end-of-input, assuming it is closing the parent top level context from bin/erubi-render:117:in 'ErubiRenderer#render_template' from bin/erubi-render:31:in 'ErubiRenderer#run' from bin/erubi-render:152:in '<main>' ``` `exe/herb render test.html.erb` ``` Hello World ```
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow up to #824 and #98.
This pull request improves the
herb_extract_ruby_to_buffer_with_semicolonsfunction even further, so that it is also be able to deal ERB comments that have a leading space on a single line:Before
After
Now, we only have a linter offense telling people to use
<%#, but no parser errors anymore.Resolves #101